package com.hbue.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hbue.bean.User;
import com.hbue.service.MemberService;
import com.hbue.service.impl.MemberServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * 用于各种用户的相关功能实现
 */

@WebServlet("/member")
public class MemberController extends HttpServlet {
    private MemberService memberService = new MemberServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理乱码问题
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //获取请求参数,判断请求的作用
        String parameter = req.getParameter("method");
        if (parameter != null & parameter.equals("login")) {
            login(req, resp);
        } else if (parameter != null && parameter.equals("findAllUser")) {
            findAllUser(req, resp);
        } else if (parameter != null && parameter.equals("deleteUser")) {
            deleteUser(req, resp);
        } else if (parameter != null && parameter.equals("findUser")) {
            findUser(req, resp);
        } else if (parameter != null && parameter.equals("update")) {
            updateUser(req, resp);
        } else if (parameter != null && parameter.equals("addUser")) {
            addUser(req, resp);
        } else if (parameter != null && parameter.equals("search")) {
            searchUser(req, resp);
        }
    }

    /**
     * 查找用户
     *
     * @param req
     * @param resp
     */
    private void searchUser(HttpServletRequest req, HttpServletResponse resp) {
        String searchContent = req.getParameter("searchContent");
        ArrayList<User> users = memberService.searchUser(searchContent);
        ServletContext context = getServletContext();
        context.setAttribute("users", users);

    }

    /**
     * 添加用户
     *
     * @param req
     * @param resp
     */
    private void addUser(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
        String Phone = req.getParameter("phone");
        User user = new User(null, username, password, name, sex, Integer.parseInt(age), Phone, 2);
        memberService.addUser(user);
    }

    /**
     * 更新用户信息
     *
     * @param req
     * @param resp
     */
    private void updateUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取JSON数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String age = req.getParameter("age");
        String Phone = req.getParameter("phone");
        User user = new User(null, username, password, name, sex, Integer.parseInt(age), Phone, 2);
        memberService.updateUser(user);
    }

    /**
     * 查询单个用户的信息
     *
     * @param req
     * @param resp
     */
    private void findUser(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String id = req.getParameter("id");
            User user = memberService.findUser(id);
            ServletContext context = getServletContext();
            context.setAttribute("member", user);
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(user);
            PrintWriter writer = resp.getWriter();
            writer.write(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void deleteUser(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        memberService.deleteUser(id);

    }

    //查询所有用户信息
    private void findAllUser(HttpServletRequest req, HttpServletResponse resp) {
        try {
            ArrayList<User> users = memberService.findAllUser();
            ServletContext context = getServletContext();
            //将查询到的数据存到context域中
            context.setAttribute("users", users);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 登录功能
     *
     * @param req
     * @param resp
     */
    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String userSign = req.getParameter("userSign");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //如果用户标记的值为1,表示为管理员登录
        if (userSign != null && userSign.equals("1")) {
            if (memberService.adminLogin(username, password)) {
                //将用户信息存入session中
                HttpSession session = req.getSession();
                session.setAttribute("username", username);
                session.setAttribute("user", "管理员");
                //登录成功,跳转至主页面
                resp.sendRedirect("page/main.jsp");
            }
        } else {
            //如果不为1,则表示为用户登录
            User user = memberService.userLogin(username, password);
            if (user != null) {
                HttpSession session = req.getSession();
                session.setAttribute("username", username);
                session.setAttribute("user", user.getName());
                //跳转主页面
                resp.sendRedirect("page/main.jsp");
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
